{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
define variable v_maillist as character no-undo.
define variable mailtitle as character.
define variable tocodepage as character.
define variable sendmail as character.
define variable mailattachment as character.
define variable reccnt as int.
define variable agingdays as int.

reccnt = 0.


sendmail = "/home/mfg/sendmailtosales.sh".
mailattachment = "/home/mfg/ARagingalert.csv".

find first code_mstr where code_domain = global_domain
                       and code_fldname = "xx_email_config"
                       and code_value = "codepage"
                       no-lock no-error.
     assign tocodepage = code_cmmt.

find first code_mstr where code_domain = global_domain
                       and code_fldname = "xx_email_config"
                       and code_value = "araginedays"
                       no-lock no-error.
     assign agingdays = int(code_cmmt).

find first code_mstr where code_domain = global_domain
                      and code_fldname = "xx_email_config"
                      and code_value = "aragingtitle "
                      no-lock no-error.


    assign mailtitle = code_cmmt.

for each code_mstr where code_domain = global_domain
                 and code_fldname = "xx_email_sales"
                 no-lock.

   v_maillist = if v_maillist = "" then  code_value
                else v_maillist + ","  +  code_value .
                 
end.



output to value(mailattachment)  convert target "CP936".   


     put unformatted getTermLabel("company",4) "," 
                    getTermLabel("customer",4) "," 
                    getTermLabel("customer",4) + getTermLabel("DESCRIPTION",9) ","
                    getTermLabel("AR_INVOICE",8) ","
                    getTermLabel("POSTING_DATE",8) ","
                    getTermLabel("INVOICE_DATE",8) ","
                    getTermLabel("CREDIT_TERMS",8) ","
                    getTermLabel("DUE_DATES",8) ","
                    getTermLabel("amount",8) 
                    skip. 
for each company no-lock,
  each dinvoice of company no-lock where dinvoice.DInvoiceDueDate - agingdays <= today
                                     and DInvoiceBalanceDebitLC - DInvoiceBalanceCreditLC > 0
                                     and dinvoicetype <> "PREPAYMENT",
   first debtor of dinvoice no-lock,
   first journal of dinvoice no-lock,
    first PaymentCondition 
            where PaymentCondition.PaymentCondition_ID = DInvoice.NormalPaymentCondition_ID.
                                     
                          
       
    reccnt = reccnt + 1.
    
   find ad_mstr where ad_domain = global_domain
                   and ad_addr = debtor.debtorcode
                   no-lock no-error.            

    put unformatted  company.companycode ","
                     debtor.debtorcode "," 
                     ad_name "," 
                    string(DInvoicePostingYear) + "/" + journal.journalcode + "/" + string(DInvoiceVoucher,"999999999") ","
                     DInvoicePostingDate ","
                     DInvoiceDate ","
                     PaymentCondition.PaymentConditionCode ","
                     DInvoiceDueDate ","
                   
                    DInvoiceBalanceDebitLC - DInvoiceBalanceCreditLC     skip.                    
end.




output close.

if reccnt > 0 then 
do:



output to value(sendmail)  convert target tocodepage.

put unformatted "/home/mfg/App-swaks-20190914.0/swaks --body " + quoter( mailtitle) +
                " --attach " + mailattachment +
                " --tls --from qad@faway.com --header " + quoter( "Subject:" + mailtitle) + 
                " --server smtp.partner.outlook.cn --port 587 " + 
                "--auth-user qad@faway.com --auth-password Faway@2024 --to ".

put unformatted v_maillist  skip.


output close.


    unix silent value("chmod 766 " + sendmail).
    unix silent value(sendmail).
    unix silent value("mv " + sendmail + " " + sendmail + ".bak").
    unix silent value("mv " + mailattachment + " " + mailattachment + ".bak").
    
end.  